package com.warm.pump.module.sys.controller;

import com.alibaba.fastjson.JSON;
import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.module.sys.bean.po.gen.DictDyna;
import com.warm.pump.module.sys.service.IDictDynaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sys_dict_dyna
 */
@Controller
@RequestMapping("/dictDyna")
public class DictDynaController {
    @Autowired
    private IDictDynaService dictDynaService;
    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("dictDyna:list")
    public OutJson list(Integer page, Integer limit, DictDyna dictDyna) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, dictDyna);
        //查询列表数据
        PageBean pageBean = dictDynaService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dictDyna:info")
    public OutJson info(@PathVariable("id") Long id) {
        DictDyna dictDyna = dictDynaService.queryObject(id);

        return OutJson.ok().put("dictDyna", dictDyna);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("dictDyna:save")
    public OutJson save(@RequestBody DictDyna dictDyna) {
        dictDyna.setStatusTime(new Date());
        dictDynaService.save(dictDyna);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("dictDyna:update")
    public OutJson update(@RequestBody DictDyna dictDyna) {
        dictDyna.setStatusTime(new Date());
        dictDynaService.update(dictDyna);

        return OutJson.ok();
    }


    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/getDictDynaByGroupCode")
    public OutJson getDictDynaByGroupCode(String groupCode, String params) {
        Map<String, Object> map = new HashMap<>();
        map.put("groupCode", groupCode);
        DictDyna dictDyna = dictDynaService.queryObject(map);
        List<Map<String, Object>> dicts = null;
        if (dictDyna != null) {
            if (1 == dictDyna.getType()) {
                String dynaSql = dictDyna.getDictTxt();
                Map<String, String> paramsMap = null;
                if (StringUtils.isNotEmpty(params)) {
                    try {
                        paramsMap = (Map<String, String>) JSON.parse(params);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return OutJson.error(params + "解析json失败，失败信息为:" + e.getMessage());
                    }
                }

                if (null != paramsMap && paramsMap.size() > 0) {
                    NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
                    dicts = namedParameterJdbcTemplate.queryForList(dynaSql, paramsMap);
                } else {
                    dicts = jdbcTemplate.queryForList(dynaSql);
                }

            }
        }

        return OutJson.ok().setResData(dicts);
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("dictDyna:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        dictDynaService.deleteBatch(ids);

        return OutJson.ok();
    }

}
